<!--地址管理-->
<template>
    <!--<view class="{{currentPage==0?'':'hidden'}}">-->
    <!--<addressList :list.sync="addressList" @currentPage.user="getCurrentPage"></addressList>-->
    <!--</view>-->
    <!--<view class="{{currentPage==1?'':'hidden'}}">-->
    <!--<addressAdd @currentPage.user="getCurrentPage"></addressAdd>-->
    <!--</view>-->
    <!--<view class="{{currentPage==2?'':'hidden'}}">-->
    <!--<addressEdit @currentPage.user="getCurrentPage" ></addressEdit>-->
    <!--</view>-->

    <view class="address">
        <view class="address_list" wx:for="{{addressList}}" wx:key="item" wx:index="index">
            <view class="title" @tap="chooseAddId({{item.id}})">
                <view class="user_info">
                    <text class="name">{{item.name}}</text>
                    <text class="phone">{{item.phone}}</text>
                </view>
                <view class="active_address">{{item.provinceName+item.cityName+item.areaName+item.address}}</view>
            </view>
            <view class="arrow" data-id="123">
                <view class="arrow_left">
                    <radio-group class="radio-group" bindchange="radioChange({{item.id}})">
                        <radio checked="{{item.defaulted}}" color="#ffb025" style="position:relative;top:-4rpx;">
                            <text id="defult" class="{{item.defaulted ? 'active' : ''}}">默认地址</text>
                        </radio>
                    </radio-group>
                </view>
                <view class="arrow_right">
                    <i class="iconfont icon-bianji" bindtap="editAddress({{item}})">编辑</i>
                    <i class="iconfont icon-unie639" bindtap="delAddress({{item.id}})">删除</i>
                </view>
            </view>
        </view>
    </view>

    <view class="add_wrap">
        <view class="button type_main" @tap="gotoAddAddressPage">新增新地址</view>
    </view>
</template>
<script>
    import wepy from 'wepy'
    import api from '../../api/api'
    import tip from '../../utils/tip'
    import {USER_SPECICAL_INFO, USER_ID} from '@/utils/constant'
    export default class Address extends wepy.page {
        config = {
            navigationBarTitleText: '我的收货地址'
        }
        data = {
            addressList: [],
            receiverInfo: {},
            //显示当前组件 0 列表 1新增 2编辑
            currentPage: 0,
            type: '' //哪个页面跳转过来的
        }

        // 删除地址
        delAddress(a) {
            let self = this
            let id = a.target.dataset.wpydeladdressA
            wx.showModal({
                title: '',
                content: '确定删除该地址？',
                showCancel: true,
                success: res => {
                    if (res.confirm) {
                        // 删除地址
                        api
                            .deleteAddress(
                                {
                                    query: {},
                                    method: 'DELETE'
                                },
                                id
                            )
                            .then(function (res) {
                                if (res.data.code == 0) {
                                    self.getUserAddress() //刷新地址列表
                                    self.$apply()
                                }
                            })
                    } else if (res.cancel) {
                        console.log('用户点击取消')
                    }
                }
            })
        }

        // 编辑地址
        editAddress(b) {
            wepy.navigateTo({
                url: '/pages/common/addressAddPage?queryObj=' + JSON.stringify(b)
            })
        }

        async getUserAddress() {
            let that = this
            let user_id = wepy.getStorageSync(USER_ID) || ''
            const json = await api.getAllList(
                {
                    query: {}
                },
                user_id
            )
            if (json.data.code == 0) {
                this.addressList = json.data.data
                that.$apply()
            } else {
                tip.error(json.data.msg)
            }
            that.showLoading = false
        }

        async receiverInf(id) {
            let that = this
            let userSpecialInfo = wepy.getStorageSync(USER_SPECICAL_INFO) || {}
            const json = await api.receiverInfoById({
                query: {
                    id: id
                }
            })
            if (json.data.code == 0) {
                this.receiverInfo = json.data.receiverInfo
                // this.$invoke("addressEdit","refresh", this.receiverInfo);
            } else {
                tip.error(json.data.msg)
            }
            that.showLoading = false
        }

        returnPage() {
            const t = this;
            if(t.type){
                if(t.type == 'purchase'||t.type == 'sellerBid'||t.type == 'purOrder'){
                    wepy.navigateBack();
                }
            }
        }
        onLoad(option) {
            this.type = option.type == undefined ? '' : option.type;
        }
        onShow() {
            this.currentPage = 0;
            this.getUserAddress();
        }

        computed = {}
        methods = {
            getCurrentPage(cur, id) {
                this.currentPage = cur
                if (cur != 1 && cur != 0) {
                    this.receiverInf(id)
                }
            },
            gotoAddAddressPage() {
                wepy.navigateTo({
                    url: '/pages/common/addressAddPage'
                })
            },
            // 点击设置默认地址
            radioChange(e) {
                let self = this
                let user_id = wepy.getStorageSync(USER_ID) || ''
                api
                    .setDefaultAddress(
                        {
                            query: {
                                appUserId: user_id,
                                addressId: e
                            },
                            method: 'PUT'
                        },
                        'appUserId=' + user_id + '&addressId=' + e
                    )
                    .then(function () {
                        self.getUserAddress(); //刷新地址列表
                        self.$apply();
                    })
            },
            // 点击设置默认地址
            chooseAddId(e) {
                wepy.setStorageSync('purOrderAddressId',e);
                this.returnPage();
            }
        }
        events = {
            refreshAddList(msg) {
                this.getUserAddress()
            }
        }
    }
</script>
<style lang="less">
.address {
    .list_box:last-child {
        border-bottom: 1px solid #efefef;
    }
    .address_list {
        margin-bottom: 40rpx;
        background: #ffffff;
        justify-content: space-between;
        // height: 200rpx;
        align-items: center;
        border-top: 1px solid #efefef;
        .title {
            border-bottom: 1px solid #efefef;
            color: #000;
            padding: 20rpx 35rpx 20rpx 35rpx;
            .user_info {
                .phone {
                    float: right;
                }
            }
        }
        .arrow {
            display: block;
            padding: 20rpx 35rpx 20rpx 35rpx;
            height: 70rpx;
            line-height: 70rpx;
            border-bottom: 1px solid #efefef;
            text-align: center;
            color: #9a9a9a;
            .arrow_left {
                float: left;
                radio {
                    .active {
                        color: #ffb025 !important;
                    }
                }
            }
            .arrow_right {
                float: right;
                .icon-unie639 {
                    margin-left: 30rpx;
                }
                .icon-bianji:before,
                .icon-unie639:before {
                    margin-right: 12rpx;
                }
            }
        }
        .user_info {
            color: #1a1a1a;
        }
        .active_address {
            margin-top: 20rpx;
        }
        #defult {
            color: #666666;
        }
    }
}
.add_wrap {
    display: block;
    width: 95%;
    left: 0;
    right: 0;
    position: absolute;
    bottom: 20rpx;
    margin: 0 auto;
    .button {
        background-color: #ffb025;
    }
}
.hidden {
    display: none;
}
</style>
